<?php 
class DanhMucDAO
{
	public static function ParseToDanhMucDTO($row)
	{
		$DanhMucDTO = new DanhMucDTO();
		$DanhMucDTO->dm_ma = $row["dm_ma"];
	//	$DanhMucDTO->dm_ma_gian_hang = $row["dm_ma_gian_hang"];
		$DanhMucDTO->dm_ten = $row["dm_ten"];
		$DanhMucDTO->dm_ma_danh_muc_cha = $row["dm_ma_danh_muc_cha"];
		return $DanhMucDTO;
	}
	
	public static function DanhMucSanPham()
	{
		$sql = "SELECT * FROM danh_muc WHERE dm_ma_danh_muc_cha = 0";
		$result = QuerySQL($sql);	
		$arrayDmDTO =NULL;
		if($result >0)
		{
		$i = 0;
		while($row = mysql_fetch_array($result))
		{
			$spDTO = DanhMucDAO::ParseToDanhMucDTO($row);
			$arrayDmDTO[$i] = $spDTO;
			$i = $i + 1;
		}
		StopConnection();
		return $arrayDmDTO;
		}
		return NULL;
	}
	
	public static function ChiTietDanhMucSanPham($MaDanhMucCha)
	{
		$sql = "SELECT * FROM danh_muc WHERE dm_ma_danh_muc_cha = '$MaDanhMucCha'";
		$result = QuerySQL($sql);
		$i = 0;
		$arrayDmDTO = NULL;
		while($row = mysql_fetch_array($result))
		{
			$spDTO = DanhMucDAO::ParseToDanhMucDTO($row);
			$arrayDmDTO[$i] = $spDTO;
			$i = $i + 1;
		}
		StopConnection();
		return $arrayDmDTO;
	}
	
	public static function DanhMucCon($MaDanhMucCha)
	{
		$sql = "SELECT * FROM danh_muc WHERE dm_ma_danh_muc_cha = '$MaDanhMucCha'";
		$result = QuerySQL($sql);
		$i = 0;
		$arrayDmDTO = NULL;
		while($row = mysql_fetch_array($result))
		{
			$spDTO = DanhMucDAO::ParseToDanhMucDTO($row);
			$arrayDmDTO[$i] = $spDTO;
			$i = $i + 1;
		}
		StopConnection();
		return $arrayDmDTO;
	}
	
	public static function HienThiTenDanhMucCon($MaDanhMuc)
	{
		$sql = "SELECT * FROM danh_muc WHERE dm_ma = '$MaDanhMuc'";
		$result = QuerySQL($sql);
		while($row = mysql_fetch_array($result))
		{
			$spDTO = DanhMucDAO::ParseToDanhMucDTO($row);
		}
		StopConnection();
		return $spDTO;
	}
	public static function CapNhatDanhMuc($TenDanhMuc,$MaDanhMuc)
	{
		$sql = "update danh_muc set dm_ten = '$TenDanhMuc' WHERE dm_ma = '$MaDanhMuc'";
		$result = QuerySQL($sql);
		StopConnection();
		return $result;
	}
	public static function ThemDanhMuc($TenDanhMuc,$MaDanhMuc)
	{
		$sql = "INSERT INTO danh_muc ( `dm_ten`, `dm_mo_ta`, `dm_ma_danh_muc_cha`) VALUES ('$TenDanhMuc', NULL, '$MaDanhMuc')";
		$result = QuerySQL($sql);
		StopConnection();
		return $result;
	}
	public static function KiemTraXoaDanhMuc($MaDanhMuc)
	{
		$sql = "SELECT * FROM danh_muc WHERE dm_ma_danh_muc_cha = '$MaDanhMuc'";
		$result = QuerySQL($sql);	
		$numrow = mysql_num_rows($result);
		//echo "<br/> DAO - SO TRA VE : ".$numrow;
		if($numrow	> 0)
		{
			return true;
		}
		return false;
	}
	public static function XoaDanhMuc($TenDanhMuc)
	{
		$sql = "delete FROM danh_muc WHERE dm_ten = '$TenDanhMuc'";
		$result = QuerySQL($sql);
		//echo $result;
	//	$numrow = mysql_num_rows($result);
		//echo "<br/> DAO - SO TRA VE : ".$numrow;
		if($result	> 0)
		{
			return true;
		}
		return false;
		
	}
	
	public static function LayMaDanhMuc($TenDanhMuc)
	{
		$sql = "SELECT * FROM danh_muc WHERE dm_ten = '$TenDanhMuc'";
		$result = QuerySQL($sql);	
		$i = 0;
		while($row = mysql_fetch_array($result))
		{
			$spDTO = DanhMucDAO::ParseToDanhMucDTO($row);
			$arrayDmDTO[$i] = $spDTO;
			$i = $i + 1;
		}
		StopConnection();
		return $arrayDmDTO;
	}
}
?>